package com.zahb.qadx.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.collection.SparseArrayCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseFragmentExt;
import com.zahb.qadx.model.Curriculum;
import com.zahb.qadx.model.CurriculumChapter;
import com.zahb.qadx.ui.adapter.ChapterAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zdw on 2020/10/21 14:01
 */
public class CurriculumTreeInfoFragment extends BaseFragmentExt {

    public ChapterAdapter mChapterAdapter;

    public static CurriculumTreeInfoFragment newInstance(Curriculum curriculum, int from) {
        Bundle args = new Bundle();
        args.putSerializable("curriculum", curriculum);
        args.putInt("from", from);
        CurriculumTreeInfoFragment fragment = new CurriculumTreeInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_curriculum_tree_info;
    }

    @Override
    protected void initViews(View rootView) {
        RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mChapterAdapter = new ChapterAdapter();
        recyclerView.setAdapter(mChapterAdapter);
        Curriculum curriculum = (Curriculum) mArguments.getSerializable("curriculum");
        if (curriculum != null) {
            if (curriculum.getCoursewareVoList() == null) return;
            if (curriculum.getCoursewareVoList().size() == 0) return;
            if (curriculum.getCoursewareVoList().get(0) == null) return;
            if (curriculum.getCoursewareVoList().get(0).getCourseOutlineList() == null) return;
            SparseArrayCompat<SparseArrayCompat<CurriculumChapter>> levelArray = recapitalizeChapterData(
                    curriculum.getCoursewareVoList().get(0).getCourseOutlineList());
            List<BaseNode> chapters = new ArrayList<>();
            SparseArrayCompat<CurriculumChapter> arrayLevel1 = levelArray.get(1);
            if (arrayLevel1 != null) {
                for (int i = 0; i < arrayLevel1.size(); i++) {
                    chapters.add(arrayLevel1.valueAt(i));
                }
            }
            mChapterAdapter.setList(chapters);
            // TODO: 2021/4/8  数据整理  
        }
    }

    private SparseArrayCompat<SparseArrayCompat<CurriculumChapter>> recapitalizeChapterData(List<CurriculumChapter> chapters) {
        // 先按照level分组
        SparseArrayCompat<SparseArrayCompat<CurriculumChapter>> levelArray = new SparseArrayCompat<>();
        for (int i = 0; i < chapters.size(); i++) {
            CurriculumChapter chapter = chapters.get(i);
            int level = chapter.getLevel();
//            Log.e("")
            SparseArrayCompat<CurriculumChapter> chapterArray = levelArray.get(level);
            if (chapterArray == null)
                chapterArray = new SparseArrayCompat<>();
            chapterArray.put(chapter.getId(), chapter);
            levelArray.put(level, chapterArray);
        }

        for (int i = 1; i < levelArray.size(); i++) {
            SparseArrayCompat<CurriculumChapter> parentArray = levelArray.get(i);
            SparseArrayCompat<CurriculumChapter> childArray = levelArray.get(i + 1);
            if (childArray != null) {
                for (int j = 0; j < childArray.size(); j++) {
                    CurriculumChapter chapterChild = childArray.valueAt(j);
                    if (parentArray != null) {
                        CurriculumChapter chapterParent = parentArray.get(chapterChild.getParentId());
                        chapterChild.setParent(chapterParent);
                        if (chapterParent != null) {
                            List<CurriculumChapter> childList = chapterParent.getChildList();
                            if (childList == null)
                                childList = new ArrayList<>();
                            childList.add(chapterChild);
                            chapterParent.setChildList(childList);
                        }
                    }
                }
            }
        }
        return levelArray;
    }
}
